﻿Imports System.Linq
Imports LomaVerde.Model

Public Class Repository(Of T As {Class, IEntity})

    Dim db As New Contexto

    Public Sub New()

    End Sub
    Public Function GetItems() As List(Of T)
        Return db.Set(Of T).ToList()
    End Function

    Public Function GetItem(ByVal id As Integer) As T
        Return db.Set(Of T).Find(id)
    End Function

    Public Sub Delete(ByVal obj As T)
        db.Set(Of T).Remove(obj)
        db.SaveChanges()
    End Sub

    Public Sub AddItem(ByVal obj As T)
        db.Set(Of T).Add(obj)
        db.SaveChanges()
    End Sub

    Public Sub EditItem(ByVal obj As T)
        db.Entry(obj).State = System.Data.EntityState.Modified
        db.SaveChanges()
    End Sub

    Public Sub Save(ByVal obj As T)

        If (obj.ID = 0) Then
            AddItem(obj)
        Else
            EditItem(obj)
        End If

    End Sub


End Class
